package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.WmsErpSourceMain;
import com.futureCloudMOM.core.mapper.WmsErpSourceMainMapper;
import com.futureCloudMOM.core.mapper.WmsErpSourceSubMapper;
import com.futureCloudMOM.core.service.WmsErpSourceMainService;
import com.futureCloudMOM.core.view.VWmsErplistview;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 【WMS_ERP_SourceMain】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-07 09:14:43
 */
@AllArgsConstructor
@Service
public class WmsErpSourceMainServiceImpl extends BaseServiceImpl<WmsErpSourceMainMapper, WmsErpSourceMain> implements WmsErpSourceMainService {

    // 源单主表
    private final WmsErpSourceMainMapper wmsErpSourceMainMapper;
    // 源单子表
    private final WmsErpSourceSubMapper wmsErpSourceSubMapper;


    @Override
    public List<VWmsErplistview> getList(QueryWrapper<VWmsErplistview> queryWrapper) {
        return wmsErpSourceMainMapper.getList(queryWrapper);
    }

    @Override
    public WmsErpSourceMain add(WmsErpSourceMain wmsErpSourceMain) {
        wmsErpSourceMainMapper.insert(wmsErpSourceMain);
        if (wmsErpSourceMain.getList().size() > 0) {
            wmsErpSourceMain.getList().forEach(item -> {
                item.setMainId(wmsErpSourceMain.getId());
                wmsErpSourceSubMapper.insert(item);
            });
        }
        return wmsErpSourceMain;
    }

    @Override
    public void edit(WmsErpSourceMain wmsErpSourceMain) {
        wmsErpSourceMainMapper.updateById(wmsErpSourceMain);
        if (wmsErpSourceMain.getList().size() > 0) {
            wmsErpSourceMain.getList().forEach(item -> {
                if (ObjectUtils.isNotEmpty(item.getId())) {
                    wmsErpSourceSubMapper.updateById(item);
                } else {
                    item.setMainId(wmsErpSourceMain.getId());
                    wmsErpSourceSubMapper.insert(item);
                }
            });
        }
        if(wmsErpSourceMain.getIds().size() > 0){
            wmsErpSourceMain.getIds().forEach(wmsErpSourceSubMapper::deleteById);
        }
    }
}
